// Sensor Sonar 
// 2010 - Interaction Source - Andre Veloso, Koji Pereira e Marcos Machado
// www.interactionsource.com

//--- Projeto Domus
// interactionsource.com
// Koji Pereira, Andre Veloso e Marcos Paulo

/*
 Projeto Domus
 interactionsource.com
 Koji Pereira, Andre Veloso e Marcos Paulo

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


// Utilizando ideias de:
// read values from a LV-MaxSonar-EZ1 sensor
// this is for the Arduino MINI -- change the pin values to suit your board.
// jet@flatline.net
// 1 Dec 2007

//Output
int statusLed = 11;

//intput
int ez1Analog = 0;

long anVolt;

float inches, cm;

float volt;

int val;

int sum = 0;    //Create sum variable so it can be averaged
int avgrange = 30;  //Quantity of values to average (sample size)

void setup() {
  pinMode(statusLed,OUTPUT);
  pinMode(ez1Analog,INPUT);
  
  analogReference(DEFAULT);
  
  Serial.begin(57600);
  
  establishContact();  // send a byte to establish contact until receiver responds 
}

void loop() {
 
  if (Serial.available() > 0) {
  
    
    int inByte = 0;         // incoming serial byte
    
    /*
    
    int val = analogRead(ez1Analog);
    
    if (val > 0) {
      // The Maxbotix reports 512 steps of information on AN
      // but we read that as 1024. Each step is 1", so we need
      // to divide by 2 to get the correct rough range in inches.
      //
      // this value should also be calibrated for your particular
      // sensor and enclosure
      //val = ((val/4.015)-256)*-1; // final
      
      //val = ((val/0.78)-256)*-1; // versao teste
      
      
      Serial.println(val); // inches
      
        }
    
    blinkLed(statusLed, 100, val);
      
      
      */
      
    //Realiza a suavizacao de amostras    
    for(int i = 0; i < avgrange ; i++)
    {
      //Used to read in the analog voltage output that is being sent by the MaxSonar device.
      //Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
      anVolt = analogRead(ez1Analog);
      sum += anVolt;
      delay(10);
    }  
        
    volt = sum/avgrange;
    
    //transforma o valor lido em dado de voltagem, para melhor conversao
    volt = volt * 5.0/1024 ;
      
    //transforma a voltagem lida em distancia em polegadas
    //de acordo com a especificacao do sensor  
    inches = (volt/0.0098) ;
    
    cm = inches * 2.54;
         
    //reset sample total
    sum = 0;
    //delay(500);
    
    val = map(cm, 0, 300, 0, 255);
    
    val = constrain(val, 0, 255);
    
    //Envia dado de distancia para a saida serial
    Serial.print(cm,BYTE);
    
    //blinkLed(statusLed, 10, 255-val);
    
    /*
    //Debugacao
    Serial.print(inches);
    Serial.print(" in, ");
    Serial.print(cm);
    Serial.print(" cm, ");
    Serial.print(val);
    Serial.print(" val");   
    
    Serial.println();  
    */
    
    delay(100); // delay before loop
    
  }
}
  
void establishContact() {
   while (Serial.available() <= 0) {
     Serial.print('A', BYTE);   // send a capital A
     delay(300);
   }
 }  
  
  
void blinkLed(int pin, int ms, int val) {
  analogWrite(pin, val); 
  delay(ms);
}


